package com.liuym.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.liuym.admin.repository.model.vo.AdminVO;
import com.liuym.admin.repository.model.vo.StudentScoreVO;
import com.liuym.admin.service.ImportAndExportService;
import com.liuym.admin.service.TableNameService;
import com.liuym.admin.util.StudentScoreTableNameUtil;
import com.liuym.common.annotation.AdminLogin;
import com.liuym.common.annotation.AdminLoginUser;
import com.liuym.common.constants.Constants;
import com.liuym.common.constants.GradeResponse;
import com.liuym.common.dubbo.student.api.StudentDubboService;
import com.liuym.common.dubbo.student.api.StudentScoreDubboService;
import com.liuym.common.dubbo.student.dto.StudentDTO;
import com.liuym.common.dubbo.student.dto.StudentScoreDTO;
import com.liuym.common.exception.StudentException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * @author Liuym
 * @date 2018/12/10 13:38
 * @describe：
 */
@AdminLogin
@RestController
@RequestMapping("/admin/studentScore")
public class StudentScoreController {

    @Reference(version = "0.0.1")
    private StudentScoreDubboService studentScoreDubboService;

    @Reference(version = "0.0.1")
    private StudentDubboService studentDubboService;

    @Autowired
    private ImportAndExportService importAndExportService;

    @Autowired
    private TableNameService tableNameService;

    private static Logger logger = LoggerFactory.getLogger(StudentScoreController.class);

    @GetMapping("/test")
    public GradeResponse test() {
        return null;
    }

    @PostMapping("/upload")
    public GradeResponse upload(MultipartFile fileUpload) {
        // 获取文件名
        String fileName = fileUpload.getOriginalFilename();
        Map<String, Object> map = null;
        return GradeResponse.success(map);
    }

    @PostMapping("/addStudentScore")
    public GradeResponse addStudentScore(StudentScoreDTO studentScoreDTO, @AdminLoginUser AdminVO adminVO) {
        try {
            studentScoreDubboService.addStudentScore(
                    StudentScoreTableNameUtil.getStudentScoreTableName("t_student_score"),
                    adminVO.getUserName(),
                    studentScoreDTO);
        } catch (StudentException e) {
            logger.warn("warn", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success();
    }

    @PostMapping("/updateStudentScore")
    public GradeResponse updateStudentScore(StudentScoreVO studentScoreVO, @AdminLoginUser AdminVO adminVO) {
        StudentScoreDTO studentScoreDTO = new StudentScoreDTO();
        if (studentScoreVO != null) {
            studentScoreDTO = BeanMapper.map(studentScoreVO, StudentScoreDTO.class);
        }
        try {
            StudentDTO studentDTO = studentDubboService.getStudentByNumber(studentScoreVO.getStudentNumber());
            studentDTO.setScore(studentDTO.getScore() - studentScoreVO.getOldScore() + studentScoreVO.getFinallyScore());
            studentDubboService.updateStudent(studentDTO);
            studentScoreDTO.setOperator(adminVO.getUserName());
            studentScoreDTO.setOperateDate(new Date());
            studentScoreDubboService.updateStudentScore(tableNameService.findByTableAndTime(
                    Constants.STUDENT_SCORE_TABLE_NAME).getTableName(),
                    studentScoreDTO);
        } catch (StudentException e) {
            e.printStackTrace();
        }
        return GradeResponse.success();
    }

    @GetMapping("/getStudentScore")
    public GradeResponse getStudentScore(int id) {
        StudentScoreVO studentScoreVO = new StudentScoreVO();
        try {
            StudentScoreDTO studentScore = studentScoreDubboService.getStudentScoreById(
                    StudentScoreTableNameUtil.getStudentScoreTableName("t_student_score"),
                    id);
            studentScoreVO = BeanMapper.map(studentScore, StudentScoreVO.class);
        } catch (StudentException e) {
            logger.warn("eror", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success(studentScoreVO);
    }

    @DeleteMapping("/deleteScore")
    public GradeResponse deleteScore(int id) {
        Integer deleteScore = null;
        try {
            StudentScoreDTO studentScoreById = studentScoreDubboService.getStudentScoreById(tableNameService.findByTableAndTime(
                    Constants.STUDENT_SCORE_TABLE_NAME).getTableName(), id);
            deleteScore = studentScoreById.getFinallyScore();
            studentScoreDubboService.deleteStudentScore(tableNameService.findByTableAndTime(
                    Constants.STUDENT_SCORE_TABLE_NAME).getTableName(), id);
        } catch (StudentException e) {
            logger.warn("eror", e);
            return GradeResponse.fail(e.getCode(), e.getMsg());
        }
        return GradeResponse.success(deleteScore);
    }
}
